$(function() {
    (function ($) {
        $.getUrlParam = function (name) {
            var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
            var r = window.location.search.substr(1).match(reg);
            if (r != null) return unescape(r[2]); return null;
        }
    })(jQuery);    
    
    var contextPath = function() {
        return  "/" + location.pathname.split("/")[1];
    }

    var lessonid = $.getUrlParam('lessonid');
	getLessonContext();
	//记录操作痕迹
    var trace_recordID = -1;
    var trace_operation = "【"+lessonid+"】";	
	// 随机数
    trace_random = (new Date()).valueOf();
	// 每10秒记录一次用户在线情况
    // 原生javascript：window.setInterval()
    // jquery调用：setInterval setTimeout(b, 500);
    //window.setInterval(function () {
	//    $.post(contextPath()+"/trace/touch",
	//   		{
	//    			"recordID":trace_recordID,
	//    			"target":location.pathname,
	//    			"operation":trace_operation
	//   		},
	//    		function(data){
	//				if (data.success) {
	//					trace_recordID = data.record_id;
	//				}
	//    		}
	//    	);
	//}, 10*1000);
	// 用户第一次访问页面记录部分数据
	//$(window).bind('onload', traceTouch);
	//window.onload = function () {
	//	traceTouch();
	//};
	// 用户继续访问根据上面提供的key值补充数据
	//$(window).bind('beforeunload', traceTouch);
	//window.onbeforeunload = function() {
	//	traceTouch();
	//};
    window.onload = traceTouch;
    window.onbeforeunload = traceTouch;
    window.setInterval(traceTouch, 3*1000);
	
	function traceTouch(){
		$.ajax({
			url : contextPath()+"/trace/touch",
			type : "get",
			data : {
				"traceRandom":trace_random,
    			"recordID":trace_recordID,
    			"target":location.pathname,
    			"operation":trace_operation
    		},
			dataType : "json",	// 数据返回类型
            cache : false, 	// 是否缓存
            async : true,    	// 默认为true 异步请求
			success : function(data) {
				if (data.success) {
					trace_recordID = data.record_id;
				}
			}
		});
	//    $.post(contextPath()+"/trace/touch",
	//    		{
	//    			"recordID":trace_recordID,
	//    			"target":location.pathname,
	//    			"operation":trace_operation
	//    		},
	//    		function(data){
	//				if (data.success) {
	//					trace_recordID = data.record_id;
	//				}
	//    		}
	//   	);
	}	
	
	function getLessonContext(e) {
		$.ajax({
			url : contextPath()+"/airuc/getlessonbyid?lessonid="+lessonid,
			type : "get",
			dataType : "json",
			success : function(data) {
				if (data.success) {
				    trace_operation += data.lesson.lessonName;	
					createPage(data.lesson);
				}
			}
		});
	}
	
	function createPage(data) {
		var html = '';
		html += "<h1>"+data.lessonName+"</h1>"
				+ "<div class='weui-panel weui-panel_access'>"
				+ "<iframe height=200 width=100% src='"+data.lessonPath+"' frameborder=0 'allowfullscreen'></iframe>"
				+ "<div class='weui-panel__bd'>"
				+ "【主讲老师】"+data.teacher+"<br>"
				+ "【讲师介绍】"+data.teacherDesc+"<br>"
				+ "【课程简介】"+data.lessonDesc+"<br>"
				+ "【课程更新】"+data.lastEditTime+"<br>"
				+ "</div>"
				+ "</div>";
		$('#tab_lesson').html(html);
	}
    

});

//function getUrlParam(name) {
//    var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)"); //构造一个含有目标参数的正则表达式对象
//    var r = window.location.search.substr(1).match(reg);  //匹配目标参数
//    if (r != null) return unescape(r[2]); return null; //返回参数值
//}
